package main

import (
	"database/sql"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"log"
	"reflect"
)

func p(i interface{}) {
	fmt.Println(i)
}
func checkErr(errMasg error) {
	if errMasg != nil {
		fmt.Println(errMasg)
		//panic(errMasg)
	}
}

var dbs = map[string]string{
	"db0": "root:aide521521@tcp(localhost:3306)/testshop?charset=utf8",
	"db1": "root:aide521521@tcp(localhost:3306)/testshop?charset=utf8",
}

func M(dbname string) *DB {
	Db := new(DB)
	_, err := sql.Open("mysql", dbs[dbname])
	if err != nil {
		panic(err)
	}
	return Db
}

type DB struct {
	dbs map[string]*sql.DB
}

func (db *DB) Select(columns string) *DB {
	if columns == "" {
		columns = "*"
	}
	return db
}
func (db *DB) From(table string) *DB {
	return db
}
func (db *DB) Join(table string, condition string) *DB {
	return db
}
func (db *DB) Where(where string, args []string) *DB {
	return db
}
func (db *DB) Group(group string) *DB {
	return db
}
func (db *DB) Having(having string) *DB {
	return db
}
func (db *DB) Order(order string) *DB {
	return db
}
func (db *DB) Limit(start int, offset int) *DB {
	return db
}
func (db *DB) Insert(maps map[string]string) *DB {
	return db
}
func (db *DB) Update(maps map[string]string) *DB {
	return db
}
func (db *DB) Delete() *DB {
	return db
}
func (db *DB) GetAll(sql string) map[int]interface{} {

	result := make(map[int]interface{})
	result[0] = map[string]string{"one": "one1", "two": "two2"}
	result[1] = map[string]string{"one": "one1", "two": "two2"}
	return result
}
func (db *DB) GetRow(sql string) map[string]string {

	result := make(map[int]map[string]string)
	result[0] = map[string]string{"one": "one1", "two": "two2"}
	result[1] = map[string]string{"one": "one1", "two": "two2"}
	return result[0]
}
func (db *DB) GetOne(sql string) map[string]string {

	result := make(map[int]map[string]string)
	result[0] = map[string]string{"one": "one1", "two": "two2"}
	result[1] = map[string]string{"one": "one1", "two": "two2"}
	return result[0]
}
func (db *DB) Query(sql string) map[int]interface{} {

	result := make(map[int]interface{})
	result[0] = map[string]string{"one": "one1", "two": "two2"}
	result[1] = map[string]string{"one": "one1", "two": "two2"}
	return result
}
func main() {
	tt := DB{"HEHE"}
	re := tt.Query("ee")
	tt.Select("")
	fmt.Println(re)
	db, err := sql.Open("mysql", "root:aide521521@tcp(localhost:3306)/testshop?charset=utf8")
	fmt.Println(reflect.TypeOf(db))
	//defer db.Close()
	if err != nil {
		log.Fatal(err)
	}
	rows, err := db.Query("select user_id,user_name from ecs_users where user_id>?", 1)
	if err != nil {
		log.Println(err)
	}
	defer rows.Close()
	columns, _ := rows.Columns()
	values := make([]sql.RawBytes, len(columns))
	scanargs := make([]interface{}, len(columns))
	for i := range values {
		scanargs[i] = &values[i]
	}
	n := 1
	var result = make(map[int]map[string]string)
	for rows.Next() {
		result[n] = make(map[string]string)
		err := rows.Scan(scanargs...)
		if err != nil {
			fmt.Println(err)
		}
		for i, v := range values {
			result[n][columns[i]] = string(v)
		}
		n++
	}
	for p, q := range result {
		fmt.Println(p)
		fmt.Println(q)
	}
	//fmt.Println(result)
}
